package org.sheep.world;

import java.util.LinkedList;
import org.sheep.R;
import org.sheep.sprite.Sprite;
import org.sheep.world.tile.Grass;
import org.sheep.world.tile.Tile;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;


public class WorldMap {
  private LinkedList<Tile>[] map = null;
  private final int xOffset = 0;
  private final int yOffset = 0;
  private final Paint p = new Paint();
  private int tileWidth;
  private int tileHeight;
  
  public Tile[] tiles = new Tile[1];
 
  public WorldMap() {
    tiles[0] = new Grass();
  }
  
	public WorldMap(LinkedList<Tile>[] map){
		this.map = map;
	}
	
	public int getTileWidth() {
	  return tileWidth;
	}
	
	public int getTileHeight() {
	  return tileHeight;
	}
	
	public void setMap(LinkedList<Tile>[] map) {
	  this.map = map;
	}
	
	public void moveLeft(int row){
		map[row].addLast(map[row].removeFirst());
	}
	
	public void moveRight(int row){
		map[row].addFirst(map[row].removeLast());
	}
 
  public void draw(Canvas canvas) {
    Bitmap im;

    for (int x = 0; x < map.length; x += 1) {
      for (int y = 0; y < map[x].size(); y += 1) {
        im = map[x].get(y).getImage();
        canvas.drawBitmap(im, xOffset+ x * im.getWidth(),
                          yOffset+ y * im.getHeight(), p);
      }
    }
  }

	public void enter(int x, int y, Sprite s) {
		map[x].get(y).enter(s);
	}
	
	public void leave(int x, int y, Sprite s) {
		map[x].get(y).leave(s);
	}

  public void setSize(int width, int height, int viewWidth, int viewHeight, Context context) {
    tileWidth = width;
    tileHeight = height;
    
    for(int i = 0; i < tiles.length; i++)
      tiles[i].setSize(width, height, viewWidth, viewHeight, context);
  }
}
